package com.fsu.database.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.fsu.database.dao.DatabaseManager;
import com.fsu.database.dao.User;

@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

	private LoginListener listener;
	
	public LoginDialog(JDialog parent, LoginListener listener) {
		super(parent);
		this.listener = listener;
		init();
	}
	
	private void init() {
		final Toolkit tk = Toolkit.getDefaultToolkit();
		final Dimension size = tk.getScreenSize();
		
		final JPanel usernamePanel = new JPanel();
		final JPanel passPanel = new JPanel();
		final JPanel buttonPanel = new JPanel();
		final JPanel errorPanel = new JPanel();
		final JPanel editPanel = new JPanel(new GridLayout(2, 1));
		
		final JLabel errorLabel = new JLabel("");
		errorLabel.setForeground(Color.RED);
		errorLabel.setVisible(false);
		
		final JLabel userLbl = new JLabel("Username: ");
		final JLabel passLbl = new JLabel("Password: ");
		final JTextField userTf = new JTextField(20);
		final JTextField passTf = new JTextField(20);
		final JButton loginBtn = new JButton("Login");
		final JButton cancelBtn = new JButton("Cancel");
		final JButton registerBtn = new JButton("Register");
		
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				errorLabel.setVisible(false);
				loginBtn.setEnabled(false);
				cancelBtn.setEnabled(false);
				registerBtn.setEnabled(false);
				
				String user = userTf.getText();
				String pass = passTf.getText();
				
				User u = null;
				
				if (user.equals("") || pass.equals("")) {
					errorLabel.setText("Username and password must not be blank.");
					errorLabel.setVisible(true);
					loginBtn.setEnabled(true);
					cancelBtn.setEnabled(true);
					registerBtn.setEnabled(true);
					return;
				}
				
				try {
					u = DatabaseManager.getUser(user);
				} catch (SQLException ex) {
					errorLabel.setText("Request Failed. Please make sure you are connected to the internet and try again.");
					errorLabel.setVisible(true);
					ex.printStackTrace();
				}
				
				if(u == null || !u.getPassword().equals(pass)) {
					errorLabel.setText("Username/Password combo does not exist.");
					errorLabel.setVisible(true);
					loginBtn.setEnabled(true);
					cancelBtn.setEnabled(true);
					registerBtn.setEnabled(true);
				} else {
					listener.loginAttempt(u.isAdmin());
					LoginDialog.this.dispose();
				}
			}
		});
		
		cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				listener.loginCanceled();
				LoginDialog.this.dispose();
			}
		});
		
		registerBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				errorLabel.setVisible(false);
				loginBtn.setEnabled(false);
				cancelBtn.setEnabled(false);
				registerBtn.setEnabled(false);
				
				String user = userTf.getText();
				String pass = passTf.getText();
				
				if(user.equals("") || pass.equals("")) {
					errorLabel.setText("Username and password must not be blank.");
					errorLabel.setVisible(true);
					loginBtn.setEnabled(true);
					cancelBtn.setEnabled(true);
					registerBtn.setEnabled(true);
					return;
				}
				User u = null;
				try {
					u = DatabaseManager.getUser(user);
					
					if(u != null) {
						errorLabel.setText("Username already exists. Please choose another.");
						errorLabel.setVisible(true);
						loginBtn.setEnabled(true);
						cancelBtn.setEnabled(true);
						registerBtn.setEnabled(true);
						return;
					}
					
					DatabaseManager.addUser(new User(user, pass, "", "", "", "", false));
					
					listener.loginAttempt(false);
					LoginDialog.this.dispose();
				
				} catch (SQLException ex) {
					errorLabel.setText("Request Failed. Please make sure you are connected to the internet and try again.");
					errorLabel.setVisible(true);
					loginBtn.setEnabled(true);
					cancelBtn.setEnabled(true);
					registerBtn.setEnabled(true);
					ex.printStackTrace();
				}
			}
		});
		
		errorPanel.add(errorLabel);
		usernamePanel.add(userLbl);
		usernamePanel.add(userTf);
		passPanel.add(passLbl);
		passPanel.add(passTf);
		editPanel.add(usernamePanel);
		editPanel.add(passPanel);
		buttonPanel.add(loginBtn);
		buttonPanel.add(registerBtn);
		buttonPanel.add(cancelBtn);
		
		add(errorPanel, BorderLayout.NORTH);
		add(editPanel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
		
		setLocation(size.width/3 + 50, size.height/3 + 50);
		setSize(400, 200);
		setVisible(true);
	}
}
